import { _decorator, Component, Node, Vec3 } from 'cc';

// 定义网格参数
export const gridConfig = {
    cellSize: 4, // 每个网格单元的大小
    width: 20,   // 网格宽度（单位：单元格）
    height: 20   // 网格高度（单位：单元格）
};


export class Grid {
    private _grid: number[][];

    constructor(width: number, height: number) {
        this._grid = new Array(width);
        for (let i = 0; i < width; i++) {
            this._grid[i] = new Array(height).fill(0); // 0表示可通行，1表示障碍
        }
    }

    // 设置单元格状态
    setCell(x: number, y: number, walkable: boolean): void {
        this._grid[x][y] = walkable ? 0 : 1;
    }

    // 获取单元格状态
    isWalkable(x: number, y: number): boolean {
        return x >= 0 && y >= 0 && x < this._grid.length && y < this._grid[0].length
            && this._grid[x][y] === 0;
    }

    // 世界坐标转网格坐标（这个有机会修改）不准确
    worldToGrid(pos: Vec3): { x: number, y: number } {
        const x1 = Math.round(pos.x / gridConfig.cellSize + gridConfig.width / 2)
        const y2 = Math.round(pos.z / gridConfig.cellSize + gridConfig.height / 2)
        return {
            x: Math.round(pos.x / gridConfig.cellSize + gridConfig.width / 2),
            y: Math.round(pos.z / gridConfig.cellSize + gridConfig.height / 2)
        };
    }

    // 网格坐标转世界坐标
    gridToWorld(x: number, y: number): Vec3 {
        return new Vec3(
            (x - gridConfig.width / 2) * gridConfig.cellSize,
            0,
            (y - gridConfig.height / 2) * gridConfig.cellSize
        );
    }



}


